package com.hoangkien.DAO;

import java.util.ArrayList;

import com.hoangkien.entity.SoundCloudEntity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class SoundCloudDAO {
	private static final String TAG = "SoundCloudDAO";
	private final SQLiteDatabase db;
	
	public SoundCloudDAO(SQLiteDatabase db) {
		super();
		this.db = db;
	}
	
	public long insert_soundcloud(SoundCloudEntity soundCloudEntity){
		ContentValues value = new ContentValues();
//		value.put(SoundCloudScheme.ID, soundCloudEntity.getId());  //primary key
		value.put(SoundCloudScheme.KIND, soundCloudEntity.getKind());
		value.put(SoundCloudScheme.CREATE_AT, soundCloudEntity.getCreated_at());
		value.put(SoundCloudScheme.USER_ID, soundCloudEntity.getUser_id());
		value.put(SoundCloudScheme.DURATION, soundCloudEntity.getDuration());
		value.put(SoundCloudScheme.COMMENTABLE, soundCloudEntity.isCommentable());
		value.put(SoundCloudScheme.STATE, soundCloudEntity.getState());
		value.put(SoundCloudScheme.ORIGINAL_CONTENT_SIZE, soundCloudEntity.getOriginal_content_size());
		value.put(SoundCloudScheme.LAST_MODIFIED, soundCloudEntity.getLast_modified());
		value.put(SoundCloudScheme.SHARING, soundCloudEntity.getSharing());
		value.put(SoundCloudScheme.TAG_LIST, soundCloudEntity.getTag_list());
		value.put(SoundCloudScheme.PERMALINK, soundCloudEntity.getPermalink());
		value.put(SoundCloudScheme.STREAM_URL, soundCloudEntity.getStream_url());
		value.put(SoundCloudScheme.EMBEDDABLE_BY, soundCloudEntity.getEmbeddable_by());
		value.put(SoundCloudScheme.DOWNLOADABLE, soundCloudEntity.isDownloadable());
		value.put(SoundCloudScheme.PURCHASE_URL, soundCloudEntity.getPurchase_url());
		value.put(SoundCloudScheme.LABLE_ID, soundCloudEntity.getLabel_id());
		value.put(SoundCloudScheme.PURCHASE_TITLE, soundCloudEntity.getPurchase_title());
		value.put(SoundCloudScheme.GENRE, soundCloudEntity.getGenre());
		value.put(SoundCloudScheme.TITLE, soundCloudEntity.getTitle());
		value.put(SoundCloudScheme.DESCRIPTION, soundCloudEntity.getDescription());
		value.put(SoundCloudScheme.LABLE_NAME, soundCloudEntity.getLabel_name());
		value.put(SoundCloudScheme.RELEASE, soundCloudEntity.getRelease());
		value.put(SoundCloudScheme.TRACK_TYPE, soundCloudEntity.getTrack_type());
		value.put(SoundCloudScheme.KEY_SIGNATURE, soundCloudEntity.getKey_signature());
		value.put(SoundCloudScheme.ISRC, soundCloudEntity.getIsrc());
		value.put(SoundCloudScheme.VIDEO_URL, soundCloudEntity.getVideo_url());
		value.put(SoundCloudScheme.BPM, soundCloudEntity.getBpm());
		value.put(SoundCloudScheme.REALEASE_YEAR, soundCloudEntity.getRelease_year());
		value.put(SoundCloudScheme.REALEASE_MONTH, soundCloudEntity.getRelease_month());
		value.put(SoundCloudScheme.REALEASE_DAY, soundCloudEntity.getRelease_day());
		value.put(SoundCloudScheme.ORIGINAL_FORMAT, soundCloudEntity.getOriginal_format());
		value.put(SoundCloudScheme.LICENSE, soundCloudEntity.getLicense());
		value.put(SoundCloudScheme.URI, soundCloudEntity.getUri());
		value.put(SoundCloudScheme.PERMALINK_URL, soundCloudEntity.getPermalink_url());
		value.put(SoundCloudScheme.ARTWORK_URL, soundCloudEntity.getArtwork_url());
		value.put(SoundCloudScheme.WAREFORM_URL, soundCloudEntity.getWaveform_url());
		value.put(SoundCloudScheme.STREAM_URL, soundCloudEntity.getStream_url());
		value.put(SoundCloudScheme.PLAYBACK_COUNT, soundCloudEntity.getPlayback_count());
		value.put(SoundCloudScheme.DOWNLOAD_COUNT, soundCloudEntity.getDownload_count());
		value.put(SoundCloudScheme.FAVORITING_COUNT, soundCloudEntity.getFavoritings_count());
		value.put(SoundCloudScheme.COMMENT_COUNT, soundCloudEntity.getComment_count());
		value.put(SoundCloudScheme.ATTACHMENTS_URI, soundCloudEntity.getAttachments_uri());
		value.put(SoundCloudScheme.POLICY, soundCloudEntity.getPolicy());
		if(db.insert(SoundCloudScheme.TABLE_SOUNDCLOUD_NAME, null, value) == -1){
			Log.e(TAG,"Bug insert soundcloud");
		} else {
			Log.e(TAG,"Successfully insert soundcloud");
		}
		return 0;
	}
	
	public ArrayList<SoundCloudEntity> getAllSoundCloud() {
		ArrayList<SoundCloudEntity> listSoundCloud = new ArrayList<SoundCloudEntity>();
		String query = "select * from " + SoundCloudScheme.TABLE_SOUNDCLOUD_NAME;
		Cursor cursor = db.rawQuery(query, null);
		if (cursor.moveToFirst()) {
			do {
				SoundCloudEntity soundCloudEntity = new SoundCloudEntity();
				soundCloudEntity.setId(cursor.getInt(0));
				soundCloudEntity.setKind(cursor.getString(1));
				soundCloudEntity.setCreated_at(cursor.getString(2));
				soundCloudEntity.setUser_id(cursor.getInt(3));
				soundCloudEntity.setDuration(cursor.getInt(4));
				soundCloudEntity.setCommentable(Boolean.parseBoolean(cursor.getString(5)));
				soundCloudEntity.setState(cursor.getString(6));
				soundCloudEntity.setOriginal_content_size(cursor.getInt(7));
				soundCloudEntity.setLast_modified(cursor.getString(8));
				soundCloudEntity.setSharing(cursor.getString(9));
				soundCloudEntity.setTag_list(cursor.getString(10));
				soundCloudEntity.setPermalink(cursor.getString(11));
				soundCloudEntity.setStreamable(Boolean.parseBoolean(cursor.getString(12)));
				soundCloudEntity.setEmbeddable_by(cursor.getString(13));
				soundCloudEntity.setDownloadable(Boolean.parseBoolean(cursor.getString(14)));
				soundCloudEntity.setPurchase_url(cursor.getString(15));
				soundCloudEntity.setLabel_id(cursor.getString(16));
				soundCloudEntity.setPurchase_title(cursor.getString(17));
				soundCloudEntity.setGenre(cursor.getString(18));
				soundCloudEntity.setTitle(cursor.getString(19));
				soundCloudEntity.setDescription(cursor.getString(20));
				soundCloudEntity.setLabel_name(cursor.getString(21));
				soundCloudEntity.setRelease(cursor.getString(22));
				soundCloudEntity.setTrack_type(cursor.getString(23));
				soundCloudEntity.setKey_signature(cursor.getString(24));
				soundCloudEntity.setIsrc(cursor.getString(25));
				soundCloudEntity.setVideo_url(cursor.getString(26));
				soundCloudEntity.setBpm(cursor.getString(27));
				soundCloudEntity.setRelease_year(cursor.getString(28));
				soundCloudEntity.setRelease_month(cursor.getString(29));
				soundCloudEntity.setRelease_day(cursor.getString(30));
				soundCloudEntity.setOriginal_format(cursor.getString(31));
				soundCloudEntity.setLicense(cursor.getString(32));
				soundCloudEntity.setUri(cursor.getString(33));
				soundCloudEntity.setPermalink_url(cursor.getString(34));
				soundCloudEntity.setArtwork_url(cursor.getString(35));
				soundCloudEntity.setWaveform_url(cursor.getString(36));
				soundCloudEntity.setStream_url(cursor.getString(37));
				soundCloudEntity.setPlayback_count(cursor.getInt(38));
				soundCloudEntity.setDownload_count(cursor.getInt(39));
				soundCloudEntity.setFavoritings_count(cursor.getInt(40));
				soundCloudEntity.setComment_count(cursor.getInt(41));
				soundCloudEntity.setAttachments_uri(cursor.getString(42));
				soundCloudEntity.setPolicy(cursor.getString(43));
				listSoundCloud.add(soundCloudEntity);
			} while (cursor.moveToNext());
		}
		return listSoundCloud;
	}
	
	public long deleteTableSoundCloud(){
		long result = 0;
		try {
			result = db.delete(SoundCloudScheme.TABLE_SOUNDCLOUD_NAME, null, null);
			Log.e(TAG,"delete table author successfully");
		} catch (Exception e) {
			Log.e(TAG,"delete table author fail");
		}
		return result;	
	}
}
